package
{
	import com.fairytask.GameStage;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import starling.core.Starling;

	[SWF(width="1000", height="600", frameRate="60", backgroundColor="#000000")]
	public class CystallBall extends Sprite
	{
		private var engin:Starling;
		private var stats:Object;
		
		public function CystallBall()
		{
			/*stats = new Stats();
			addChild(stats);*/
			if(this.stage)
			{
				this.stage.scaleMode = StageScaleMode.NO_SCALE;
				this.stage.align = StageAlign.TOP_LEFT;
			}
			engin	=	new Starling(GameStage,stage);
			engin.antiAliasing = 1;
			engin.start();
			GameStage.instance	=	engin.root as GameStage;
			
			//事件处理
			this.stage.addEventListener(Event.RESIZE, stage_resizeHandler, false, int.MAX_VALUE, true);
			this.stage.addEventListener(Event.DEACTIVATE, stage_deactivateHandler, false, 0, true);
		}
		
		private function stage_resizeHandler(event:Event):void
		{
			this.engin.stage.stageWidth = this.stage.stageWidth;
			this.engin.stage.stageHeight = this.stage.stageHeight;
			
			const viewPort:Rectangle = this.engin.viewPort;
			viewPort.width = this.stage.stageWidth;
			viewPort.height = this.stage.stageHeight;
			try
			{
				this.engin.viewPort = viewPort;
			}
			catch(error:Error) {}
		}
		
		private function stage_deactivateHandler(event:Event):void
		{
			this.engin.stop();
			this.stage.addEventListener(Event.ACTIVATE, stage_activateHandler, false, 0, true);
		}
		
		private function stage_activateHandler(event:Event):void
		{
			this.stage.removeEventListener(Event.ACTIVATE, stage_activateHandler);
			this.engin.start();
		}
	}
}